/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000 Intel Corporation. All Rights Reserved.
//
//  VSS: 
//     $Workfile: $
//     $Revision: $
//     $Date:     $
//     $Archive:  $
//
//  Description:
//      Intel(R) Integrated Performance Primitives - JPEG codec file.
//      This header file contains internal hufman decoding definitions.
//
//  History:
//     Date        Author        Changes
//     2000/5/14   Jerry Hu      Created  (Version 0.1)
//     2000/12/22  Jerry Hu      Modified (Version 0.2)
//
********************************************************************************/
#ifndef _IJDHUFSH_H
#define _IJDHUFSH_H

#include "_ippjpeg.h"

extern const int _nLowestCoef[];

/* Internal structure definition for Bit stream */
typedef struct _STREAM
{
    /* Identifies the start address of stream buffer */
    const Ipp8u *pHeadStreamBuffer;
    /* Identifies the current address of stream buffer */
    const Ipp8u *pStreamBuffer;
    /* Specifies the current used length in Ipp8u */
    int nUsedBytesLen; 

    /* Specifies preview bits buffer */
    Ipp32u *pnPrefetchedBits;
    /* Specifies the number of valid bits in "nPrefetchedBits" */
    int *pnNumValidBits;

    /* Specifies the marker value if it was find by "_FillPreviewBits"*/
    int *pMarker;
}_stream;

/* 
// Copy "nNumBits" bits from "Bits Preview Buffer" to "result",  
// Reduce "nNumValidBits".
*/
#define _GETBITS(result, nNumBits, pStream)             \
{                                                       \
    register unsigned int r = ((1<<(nNumBits))-1);      \
    (*((pStream)->pnNumValidBits)) -= (nNumBits);       \
    result=(((int)((*((pStream)->pnPrefetchedBits))>>(*((pStream)->pnNumValidBits)))) & r); \
}

/* 
// Copy "nNumBits" bits to result from "Bits Preview Buffer",  
// Do not reduce "nNumValidBits".
*/

/* pStream->nNumValidBits > nNumBits */
#define _PEEKBITS_L(result, nNumBits, pStream)                    \
{                                                                 \
    register int r = ((1 << (nNumBits)) - 1);                     \
    register int t = ((int)((*((pStream)->pnNumValidBits))-(nNumBits)));\
    result=(((int)((*((pStream)->pnPrefetchedBits))>>(t))) & r); \
}

/* pStream->nNumValidBits < nNumBits */
#define _PEEKBITS_S(result, nNumBits, pStream)                    \
{                                                                 \
    register int r = ((1 << (nNumBits)) - 1);                     \
    register int t = ((int)((nNumBits)-(*((pStream)->pnNumValidBits))));\
    result=(((int)((*((pStream)->pnPrefetchedBits))<<(t))) & r);      \
}

/* Check valid bit length in the current preview buffer */
#define _CHECKPREVIEWBITSNUM(nRefNumBits, pStream, errCode)     \
{                                                               \
    if (*((pStream)->pnNumValidBits)<(nRefNumBits)) {           \
        if (0>=_IppPrefetchBits(pStream)) {                        \
            return (errCode);                                   \
        }                                                       \
    }                                                           \
}
                                       
int _IppPrefetchBits(_stream *pStream);

int _IppDecodeLongBits(int  nNumValidBits,
                    const IppJPEGDeHuffTable *pHuffTable,
                    _stream *pStream);

int _IppHuffDecode(const IppJPEGDeHuffTable *pHuffTable,
                _stream *pStream);

#endif
/* EOF */
